{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# By Jinjun Wu\n",
    "# make a data_list and label_list\n",
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "import os\n",
    "\n",
    "#%%\n",
    "\n",
    "# you need to change this to your data directory\n",
    "\n",
    "def get_files(file_dir):\n",
    "    '''\n",
    "    Args:\n",
    "        file_dir: file directory\n",
    "    Returns:\n",
    "        list of images and labels\n",
    "    '''   \n",
    "    image_list = []\n",
    "    label_list = []\n",
    "    for file in os.listdir(file_dir):\n",
    "        data_path = file_dir+file\n",
    "        data_label=int(data_path.split(sep = \"class\")[1][0:2])-1  #1-10 --> 0-9        print(file_dir+file)\n",
    "        image_list.append(data_path)\n",
    "        label_list.append(data_label)\n",
    "    \n",
    "    return image_list, label_list\n",
    "\n",
    "\n",
    "data_dir = \"/home/gps/HDD/dataset_dzkd_radar0612/trains/\"\n",
    "image_list, label_list=get_files(data_dir)\n",
    "def get_batch(image, label, image_W, image_H, batch_size, capacity):\n",
    "    '''\n",
    "    Args:\n",
    "        image: list type\n",
    "        label: list type\n",
    "        image_W: image width\n",
    "        image_H: image height\n",
    "        batch_size: batch size\n",
    "        capacity: the maximum elements in queue\n",
    "    Returns:\n",
    "        image_batch: 4D tensor [batch_size, width, height, 3], dtype=tf.float32\n",
    "        label_batch: 1D tensor [batch_size], dtype=tf.int32\n",
    "    '''\n",
    "    \n",
    "    image = tf.cast(image, tf.string)\n",
    "    label = tf.cast(label, tf.int32)\n",
    "\n",
    "    # make an input queue\n",
    "    input_queue = tf.train.slice_input_producer([image, label])\n",
    "    label = input_queue[1]\n",
    "    image_contents = tf.read_file(input_queue[0])\n",
    "    image = tf.image.decode_jpeg(image_contents, channels=3)    \n",
    "    #image = tf.image.decode_jpeg(tf.read_file(\"/home/wjj/My-TensorFlow-tutorials-master/cats_dogs/data/trains/dog.10004.jpg\"), channels=3)\n",
    "\n",
    "\n",
    "    ######################################\n",
    "    # data argumentation should go to here\n",
    "    ######################################\n",
    "    \n",
    "    image = tf.image.resize_image_with_crop_or_pad(image, image_W, image_H)\n",
    "\n",
    "    # if you want to test the generated batches of images, you might want to comment the following line.\n",
    "    image = tf.image.per_image_standardization(image)\n",
    "    \n",
    "    image_batch, label_batch = tf.train.batch([image, label],\n",
    "                                                batch_size= batch_size,\n",
    "                                                num_threads= 64,\n",
    "                                                capacity = capacity)\n",
    "    \n",
    "    #you can also use shuffle_batch \n",
    "#    image_batch, label_batch = tf.train.shuffle_batch([image,label],\n",
    "#                                                      batch_size=BATCH_SIZE,\n",
    "#                                                      num_threads=64,\n",
    "#                                                      capacity=CAPACITY,\n",
    "#                                                      min_after_dequeue=CAPACITY-1)\n",
    "\n",
    "    ## ONE-HOT      \n",
    "    n_classes = 10\n",
    "    label_batch = tf.one_hot(label_batch, depth= n_classes)\n",
    "    label_batch = tf.cast(label_batch, dtype=tf.int32)\n",
    "    label_batch = tf.reshape(label_batch, [batch_size, n_classes])\n",
    "    \n",
    "    \n",
    "    \n",
    "#     label_batch = tf.reshape(label_batch, [batch_size])\n",
    "#     image_batch = tf.cast(image_batch, tf.float32)\n",
    "    \n",
    "    return image_batch, label_batch"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
